from datetime import date
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client
from studenthoods.apps.shop.models import Shop, Order, HoodieSize, HoodieColour
from django.template.defaultfilters import slugify
from studenthoods.apps.shop.utils import generate_shop_slug_hash

import random

class ShopTest(TestCase):
    def setUp(self):
        #management.call_command('flush', interactive=False, verbosity=1)
        self.client = Client()
        
    def test_shop(self):
        shop = create_shop()
        
        response = self.client.get(shop.get_absolute_url())
        self.assertContains(response, '', None, status_code=200)
        
    def test_shop_fail_bad_name(self):
        shop = create_shop()
        
        response = self.client.get('shop/blah-blah/')
        self.assertContains(response, '', None, status_code=404)
    
class OrderTest(TestCase):
    def setUp(self):
        self.client = Client()
    
    def test_order_successful_with_address(self):
        shop = create_shop(True)
        colour = create_colour("Black", "000000", shop)
        size = create_size("S", colour)
        
        response = self.client.get(shop.get_absolute_url())
        self.assertContains(response, '', None, status_code=200)
        
        response = self.client.post(shop.get_absolute_url(), {
                'first_name': 'Joe',
                'surname': 'Bloggs',
                'email': 'test@example.org',
                'phone_number':  '01225874631',
                'address_line_1': '1 Example Road',
                'address_line_2': 'Hackney',
                'city': 'Dalston',
                'postcode': 'E48EE',
                'size': size.size,
                'colour': colour.colour,
                'terms': True,
                'slug_hash':shop.slug_hash})
        
        print response
        
        self.assertContains(response, '', None, status_code=302)

    def test_order_unsuccessful_with_address_due_to_invalid_size_for_colour(self):
        shop = create_shop(True)
        colour = create_colour("White", "ffffff", shop)
        size = create_size("L", colour)

        fake_colour = create_colour("Red", "ff0000", shop)
        fake_size = create_size("S", fake_colour)

        response = self.client.get(shop.get_absolute_url())
        self.assertContains(response, '', None, status_code=200)
        
        response = self.client.post(shop.get_absolute_url(), {
                'first_name': 'Joe',
                'surname': 'Bloggs',
                'email': 'test@example.org',
                'phone_number':  '01225874631',
                'address_line_1': '1 Example Road',
                'address_line_2': 'Hackney',
                'city': 'Dalston',
                'postcode': 'E48EE',
                'size': 'S',
                'colour': colour.colour,
                'terms': True,
                'slug_hash':shop.slug_hash})
                
        self.assertContains(response, '', None, status_code=200)
        
        self.assertFormError(
                        response,
                        'form',
                        'size',
                        'Please select a valid Size for your Colour choice'
                )
    def test_order_successful_without_address(self):
        shop = create_shop(False)
        colour = create_colour("Black", "000000", shop)
        size = create_size("S", colour)
        
        response = self.client.get(shop.get_absolute_url())
        self.assertContains(response, '', None, status_code=200)
        
        response = self.client.post(shop.get_absolute_url(), {
                'first_name': 'Joe',
                'surname': 'Bloggs',
                'email': 'test@example.org',
                'phone_number':  '01225874631',
                'size': size.size,
                'colour': colour.colour,
                'terms': True,
                'slug_hash':shop.slug_hash})

        self.assertContains(response, '', None, status_code=302)
        
    def test_order_unsuccessful_bad_email(self):
        shop = create_shop()
        colour = create_colour("Red", "FF0000", shop)
        size = create_size("M", colour)
        
        response = self.client.get(shop.get_absolute_url())
        self.assertContains(response, '', None, status_code=200)
        
        response = self.client.post(shop.get_absolute_url(), {
                'first_name': 'Joe',
                'surname': 'Bloggs',
                'email': 'testexampleorg',
                'phone_number':  '01225874631',
                'address_line_1': '1 Example Road',
                'address_line_2': 'Hackney',
                'city': 'Dalston',
                'postcode': 'E48EE',
                'size': size.size,
                'colour': colour.colour,
                'terms': True,
                'slug_hash':shop.slug_hash})
                
        self.assertContains(response, '', None, status_code=200) 
        
    def test_order_unsuccessful_bad_email(self):
        shop = create_shop()
        colour = create_colour("Yellow", "FFFF00", shop)
        size = create_size("L", colour)
        
        response = self.client.get(shop.get_absolute_url())
        self.assertContains(response, '', None, status_code=200)
        
        response = self.client.post(shop.get_absolute_url(), {
                'first_name': 'Joe',
                'surname': 'Bloggs',
                'email': 'testexampleorg',
                'phone_number':  '01225874631',
                'address_line_1': '1 Example Road',
                'address_line_2': 'Hackney',
                'city': 'Dalston',
                'postcode': 'E48EE',
                'size': size.size,
                'colour': colour.colour,
                'terms': False,
                'slug_hash':shop.slug_hash})
                
        self.assertContains(response, '', None, status_code=200)
        
    def test_ajax_size_page(self):
        shop = create_shop()
        colour = create_colour("Grey", "666666", shop)
        size = create_size("S", colour)
        size = create_size("M", colour)
        size = create_size("L", colour)
        
        response = self.client.get(colour.get_sizes_url())
        self.assertContains(response, '', None, status_code=200)
        
        self.assertEqual(len(response.context['sizes']), 3)
        
def create_shop(address=False):
    shop = Shop()
    shop.name = 'Shop '+ str(random.random())
    shop.slug = slugify(shop.name)
    shop.description = 'blah blah blah blah blah'
    shop.opening_date = date.today()
    shop.closing_date = date.today()
    shop.order_target = 45
    shop.price = '12.99'
    shop.address_required = address
    shop.slug_hash = generate_shop_slug_hash(shop.slug)
    shop.save()
    return shop

def create_colour(colour_name, rgb, shop):
    colour =  HoodieColour()
    colour.colour = colour_name
    colour.slug = slugify(colour_name)
    colour.rgb = rgb
    colour.shop = shop
    colour.save()
    return colour

def create_size(size_name, colour):
    size = HoodieSize()
    size.size = size_name
    size.colour = colour
    size.save()
    return size